package com.ruoyi.project.system.submit.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.system.job.domain.JobCourseClass;
import com.ruoyi.project.system.job.mapper.JobCourseClassMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.submit.mapper.JobSubmitMapper;
import com.ruoyi.project.system.submit.domain.JobSubmit;
import com.ruoyi.project.system.submit.service.IJobSubmitService;
import com.ruoyi.common.utils.text.Convert;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 作业-提交记录Service业务层处理
 *
 * @author ruoyi
 * @date 2021-04-20
 */
@Service
public class JobSubmitServiceImpl implements IJobSubmitService
{
    @Autowired
    private JobSubmitMapper jobSubmitMapper;

    @Autowired
    private JobCourseClassMapper jobCourseClassMapper;
    /**
     * 查询作业-提交记录
     *
     * @param id 作业-提交记录ID
     * @return 作业-提交记录
     */
    @Override
    public JobSubmit selectJobSubmitById(Long id)
    {
        return jobSubmitMapper.selectJobSubmitById(id);
    }

    /**
     * 查询作业-提交记录列表
     *
     * @param jobSubmit 作业-提交记录
     * @return 作业-提交记录
     */
    @Override
    public List<JobSubmit> selectJobSubmitList(JobSubmit jobSubmit)
    {
        return jobSubmitMapper.selectJobSubmitList(jobSubmit);
    }

    /**
     * 新增作业-提交记录
     *
     * @param jobSubmit 作业-提交记录
     * @return 结果
     */
    @Override
    public int insertJobSubmit(JobSubmit jobSubmit)
    {
        jobSubmit.setCreateTime(DateUtils.getNowDate());
        return jobSubmitMapper.insertJobSubmit(jobSubmit);
    }

    /**
     * 修改作业-提交记录
     *
     * @param jobSubmit 作业-提交记录
     * @return 结果
     */
    @Override
    public int updateJobSubmit(@RequestBody JobSubmit jobSubmit)
    {
        Long userId = ShiroUtils.getUserId();
        if(jobSubmit.getJobCourseClassList().size() > 0){
            for (JobCourseClass courseClass : jobSubmit.getJobCourseClassList()) {
                if(courseClass.getScore().compareTo(new BigDecimal("0")) == 0){
                    continue;
                }
                if(courseClass.getMakeUpTime()!=null && DateUtils.getNowDate().after(courseClass.getMakeUpTime())){
                    throw new BusinessException("作业提交已到期，请联系对应老师");
                }
                JobSubmit condition = new JobSubmit();
                condition.setJobId(courseClass.getId());
                condition.setStudentId(userId);
                List<JobSubmit> jobSubmitList = jobSubmitMapper.selectJobSubmitList(condition);
                if(jobSubmitList.size() > 0){
                    boolean flag = true;
                    for (JobSubmit submit : jobSubmitList) {
                        submit.setStatus(1);
                        submit.setUpdateTime(DateUtils.getNowDate());
                        submit.setEnclosureUrl(courseClass.getYouranswer());
                        if(flag){
                            if(courseClass.getType().equals("选择题") || "填空题".equals(courseClass.getType())){
                                if(StringUtils.isNotEmpty(courseClass.getYouranswer()) && courseClass.getYouranswer().equals(courseClass.getAnswer())){
                                    submit.setScore(courseClass.getScore());
                                }
                            }
                            flag = false;
                        }
                        jobSubmitMapper.updateJobSubmit(submit);

                    }
                }else {
                    throw new BusinessException("请使用对应学生账号进行答题");
                }
            }
        }
        // 如果是选择和填空自动计算得分
        return 1;
    }

    /**
     * 删除作业-提交记录对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteJobSubmitByIds(String ids)
    {
        return jobSubmitMapper.deleteJobSubmitByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除作业-提交记录信息
     *
     * @param id 作业-提交记录ID
     * @return 结果
     */
    @Override
    public int deleteJobSubmitById(Long id)
    {
        return jobSubmitMapper.deleteJobSubmitById(id);
    }

    @Override
    public int piyue(JobSubmit jobSubmit) {
        if(jobSubmit.getJobCourseClassList().size() > 0){
            for (JobCourseClass courseClass : jobSubmit.getJobCourseClassList()) {
                JobSubmit condition = new JobSubmit();
                condition.setJobId(courseClass.getId());
                condition.setStudentId(jobSubmit.getStudentId());
                List<JobSubmit> jobSubmitList = jobSubmitMapper.selectJobSubmitList(condition);
                if(jobSubmitList.size() > 0){
                    boolean flag = true;
                    for (JobSubmit submit : jobSubmitList) {
                        submit.setStatus(2);
                        submit.setUpdateTime(DateUtils.getNowDate());
                        if(flag){
                            if(courseClass.getActualScore() != null){
                                submit.setScore(courseClass.getActualScore());
                            }
                            flag = false;
                        }
                        jobSubmitMapper.updateJobSubmit(submit);

                    }
                }
            }
        }
        // 如果是选择和填空自动计算得分
        return 1;
    }
}
